<?php

require_once TEMPLATEPATH . '/helpers/common.php';

add_shortcode('dktm_box', 'dktm_box_function');
add_shortcode('dktm_forex', 'GetDktmForex');
add_shortcode('dktm_goldprice', 'GetDktmGoldPrice');
add_shortcode('current_time', 'current_time_func');
add_shortcode('current_date', 'current_date_func');

add_shortcode('dktm_popup', 'dktm_popup_func');
add_shortcode('dktm_share_information', 'dktm_share_information_func');
add_shortcode('dktm_share_content', 'dktm_share_content_func');
add_shortcode('dktm_social_share', 'dktm_social_share_func');

add_shortcode('content_gap', 'content_gap_func');
add_shortcode('bar', 'bar_func');

function dktm_social_share_func($atts) {
    $output = dktm_share();
    return $output;
}

function bar_func($atts) {
    extract(shortcode_atts(array(
        'width' => '1',
        'color' => 'CFCBCB',
                    ), $atts));
    //return '';
    $output = "<hr style='margin:15px 0;padding:15px 0;border:0; width:100%; clear:both; border-bottom:{$width}px solid #{$color};display:block;' />";
    return $output;
}

function content_gap_func($atts, $content = null) {
    extract(shortcode_atts(array(
        'tag' => 'div',
        'gap_type' => 'padding',
        'top' => '10px',
        'right' => '10px',
        'bottom' => '10px',
        'left' => '10px',
                    ), $atts));

    $output = "<{$tag} style='{$gap_type}:{$top} {$right} {$bottom} {$left}; '>";
    $output .= ($content != "" || $content != null) ? $content : "";
    $output .= "</{$tag}>";
    return $output;
}

function current_date_func($atts) {
    return "<span>" . date('Y-m-d',  current_time('timestamp')) . "</span>";
}

function current_time_func($atts) {
    return "<span>" . date('h:i:s',current_time('timestamp')) . "</span>";
}

function dktm_box_function($atts, $content = null) {
    extract(shortcode_atts(array(
        'title' => '',
        'icon' => '',
        'template' => 'default',
        'color' => '#2D86C9', //color
        'image' => '', //image
        'link' => '#', //image
        'repeat' => 'repeat', //repeat|repeat-x|repeat-y|no-repeat
        'show' => 5
                    ), $atts));
    $content = ($content == null) ? "" : $content;
    $data = array('title' => $title,'icon' => $icon,'template' => $template, 'color' => $color, 'image' => $image, 'repeat' => $repeat, 'link' => $link, 'show' => $show, 'content' => $content);
    $output = capture_template($template, $data);
    return $output;
}

function dktm_share_content_func($atts, $content = null) {
//    return '';
    extract(shortcode_atts(array(
        'title' => 'HAVE SOME NEWS: Please Share',
        'type' => 'information',
                    ), $atts));
    $content = ($content == null) ? "" : $content;
    $data = array('title' => $title, 'type' => $type, 'content' => $content);
    $output = capture_template('share_template', $data);
    return $output;
}

function dktm_share_information_func($atts, $content = null) {
//    return '';
    extract(shortcode_atts(array(
        'title' => 'HAVE SOME NEWS: Please Share.',
                    ), $atts));
    $content = ($content == null) ? "Let’s be an online news reporter! If you happen to know anything in your area or nearby, please share with us. Help us out in reaching every part of the country." : $content;
    $data = array('title' => $title, 'content' => $content);
    $output = capture_template('share_information', $data);
    return $output;
}

function dktm_popup_func($atts, $content = null) {
    extract(shortcode_atts(array(
        'title' => '',
        'curved' => false,
        'post_type' => 'page',
        'id' => '',
        'ajax' => 1,
        'url' => '',
        'new_tab' => 0,
                    ), $atts));
    $output = "";
    if ($title == "")
        $title = "more";
    $class = ($curved) ? "curved" : "";
    if ($id > 0) {
        if ($ajax != 1) {
            $unique_id = "inline_" . strtotime(date('Y-m-d'));
            $output .= "<a href='#" . $unique_id . "' class='inline_popup_fix'><h1 class='{$class} widget-title'>{$title}</h1>";
            $output .="<div style='display:none;'>";
            if ($post_type == "page") {
                $args = array(
                    'post_type' => array('page'),
                    'page_id' => $id,
                );
            } else {
                $args = array(
                    'p' => $id,
                );
            }
            $custom_query = new WP_Query($args);
            while ($custom_query->have_posts()) : $custom_query->the_post();
                $output .= '<div id="' . $unique_id . '" class="colorbox_popup_wrapper">';
                $output .= do_shortcode(get_the_content());
                $output .= '</div>';
            endwhile;
            wp_reset_query();
            $output .="</div>";
        }else {
            $output .= "<a href='" . admin_url("admin-ajax.php?action=colorbox_popup&id={$id}&post_type={$post_type}") . "' class='ajax_popup'><h1 class='{$class} widget-title'>{$title}</h1>";
        }
        if ($content != "") {
            $output .="<p class='popup_description'>{$content}</p>";
        }
        $output .="</a>";
    } else if ($url != "") {
        if ($new_tab == 1) {
            $output .= "<a href='" . $url . "' target='_blank'><h1 class='{$class} widget-title'>{$title}</h1>";
        } else {
            $output .= "<a href='" . $url . "' class='iframe_popup'><h1 class='{$class} widget-title'>{$title}</h1>";
        }

        if ($content != "") {
            $output .="<p class='popup_description'>{$content}</p>";
        }
        $output .="</a>";
    }
    return $output;
}

function GetDktmForex() {
    $page = @file_get_contents('http://www.nrb.org.np/index.php');
    $doc = new DOMDocument();
    $doc->validateOnParse = true;
    $status = @$doc->loadHTML($page);
    $prepend = '<tr><td colspan="3"><a href="http://www.nrb.org.np/fxmexchangerate1.php?YY=&&MM=&&DD=" target="_blank">[more]</a>';
    $forexData = '';
    if ($status) {
        $tables = $doc->getElementsByTagName('table');

        $cnt = 0;
        foreach ($tables as $table) {
            if ($table->getAttribute('class') === 'tbl') {
                $forexData .= '<table class="forex_widget"><tbody>';
                $nodes = $table->childNodes;
                foreach ($nodes as $node) {
                    $cnt++;
                    if ($cnt == 1 || $cnt == 9) //forex title & more link
                        continue;
                    else {
                        $childNodes = $node->childNodes;
                        $nodeValue = $node->nodeValue;
                        if ($cnt == 2) {
                            $trClass = "forex_first";
                        } else if ($cnt == 3) {
                            $trClass = "forex_second";
                        } else {
                            $trClass = '';
                        }
                        $forexData .= "<tr class='$trClass'>";
                        if ($cnt == 2) {
                            $forexData .= "<td colspan='3'>" . $nodeValue . "</td>";
                        } else {
                            foreach ($childNodes as $childNode) {
                                $childNodeValue = $childNode->nodeValue;
                                if (trim($childNodeValue) == "") {
                                    continue;
                                }
                                $forexData .= "<td>" . $childNodeValue . "</td>";
                            }
                        }
                        $forexData .= "</tr>";
                    }
                }
                $forexData .= $prepend . '</tbody></table>';
            }
        }
    }
    return $forexData;
    //exit;
}

function GetDktmGoldPrice() {
    $page = @file_get_contents('http://sharesansar.com/listGoldSilver.php');
    $doc = new DOMDocument();
    $doc->validateOnParse = true;
    $maxPrice = 2;
    $status = @$doc->loadHTML($page);
    $prepend = '<tr><td colspan="3"><a href="http://sharesansar.com/listGoldSilver.php" target="_blank">[more]</a>';
    $forexData = '';
    if ($status) {
        $tables = $doc->getElementsByTagName('table');

        $cnt = 0;
        foreach ($tables as $table) {
            if ($table->getAttribute('width') === '500px') {
                $forexData .= '<table class="goldprice_widget"><tbody>';
                $nodes = $table->childNodes;
                foreach ($nodes as $node) {
                    $cnt++;
                    if ($cnt > ($maxPrice + 1))
                        continue;
                    $childNodes = $node->childNodes;
                    $nodeValue = $node->nodeValue;
                    if ($cnt == 1) {
                        $trClass = "gold_first";
                    } else if ($cnt == 2) {
                        $trClass = "gold_second";
                    } else {
                        $trClass = '';
                    }

                    $forexData .= "<tr class='$trClass'>";
                    //$forexData .= "<td colspan='3'>" . $nodeValue . "</td>";
                    $cntPrice = 0;
                    foreach ($childNodes as $childNode) {
                        $cntPrice++;
                        $childNodeValue = $childNode->nodeValue;
                        if (trim($childNodeValue) == "") {
                            continue;
                        }
//                        if($cnt>1 && $cntPrice==1){
//                            $temp = explode('-', $childNodeValue);
//                            print_r($temp);
//                            $year = floatval($temp[0]);
//                            $month = floatval($temp[1]);
//                            $day = floatval($temp[2]);
//                            $timestamp=mktime(0, 0,0, $month, $day, $year);
//                            $childNodeValue = date("m F, Y", $timestamp);
//                        }
                        $forexData .= "<td>" . $childNodeValue . "</td>";
                    }
                    $forexData .= "</tr>";
                }
                $forexData .= $prepend . '</tbody></table>';
            }
        }
    }
    return $forexData;
    //exit;
}

?>
